package com.haust.su.ghyy.hospital.controller.api;

import com.haust.su.ghyy.common.exception.YyghException;
import com.haust.su.ghyy.common.helper.HttpRequestHelper;
import com.haust.su.ghyy.common.result.Result;
import com.haust.su.ghyy.common.result.ResultCodeEnum;
import com.haust.su.ghyy.common.utils.MD5;
import com.haust.su.ghyy.hospital.service.HospitalSetService;
import com.haust.su.ghyy.hospital.service.ScheduleService;
import com.haust.su.ghyy.model.hosp.Department;
import com.haust.su.ghyy.model.hosp.Schedule;
import com.haust.su.ghyy.vo.hosp.DepartmentQueryVo;
import com.haust.su.ghyy.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author haustsusu
 * @create 2023-04-27-15:05
 */
@RestController
@RequestMapping("/api/hosp")
public class ScheduleController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private ScheduleService scheduleService;


    //删除排班的接口
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> Parammap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode =(String)Parammap.get("hoscode");

        //获取排班编号
        String hosScheduleId = (String)Parammap.get("hosScheduleId");

        //获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign=(String)Parammap.get("sign");

        //根据传递过来的医院编号,查询数据库,查询签名
        String signKey=hospitalSetService.getSignKey(hoscode);

        //吧数据库查询的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        //加入不一致
        if(!hospSign.equals(signKeyMd5)){
            //加入签名不一致,爆出签名不一致异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //签名正确后进行删除操作
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();


    }



    //分页查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> Parammap = HttpRequestHelper.switchMap(requestMap);

        //当前页与每页数量
        int page= StringUtils.isEmpty(Parammap.get("page")) ?1:Integer.parseInt((String) Parammap.get("page"));
        int limit= StringUtils.isEmpty(Parammap.get("limit")) ?1:Integer.parseInt((String) Parammap.get("limit"));

        //进行签名的校验
        //获取医院编号
        String hoscode =(String)Parammap.get("hoscode");

        //获取科室编号
        String depcode = (String)Parammap.get("depcode");

        //获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign=(String)Parammap.get("sign");

        //根据传递过来的医院编号,查询数据库,查询签名
        String signKey=hospitalSetService.getSignKey(hoscode);

        //吧数据库查询的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        //加入不一致
        if(!hospSign.equals(signKeyMd5)){
            //加入签名不一致,爆出签名不一致异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageResult=scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageResult);


    }


    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> Parammap = HttpRequestHelper.switchMap(requestMap);

        //进行签名的校验
        //获取医院编号
        String hoscode =(String)Parammap.get("hoscode");
        //获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign=(String)Parammap.get("sign");

        //根据传递过来的医院编号,查询数据库,查询签名
        String signKey=hospitalSetService.getSignKey(hoscode);

        //吧数据库查询的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        //加入不一致
        if(!hospSign.equals(signKeyMd5)){
            //加入签名不一致,爆出签名不一致异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(Parammap);
        return Result.ok();
    }
}
